<?php
namespace app\sysadmin\controller;
use think\Db;
use Wy\Content as WyContent;
use Wy\Attr;
class Content extends Common{
	private $wy_content_api = '';
	public function __construct(){
		parent::__construct();
		$this->wy_content_api = new WyContent;
	}
	/*
	 * 内容管理主页
	 * 	左侧显示目录
	 *  右侧显示内容
	 */
	public function index(){
		$_data = $this->wy_content_api->get_cat_lists();
		$this->assign('menu_lists',$_data);
		return $this->fetch();
	}
	/*
	 * 列表管理
	 * 全部内容列表管理
	 * 选择对应模型管理里面的模型
	 */
	public function lists(){
		$_input_data = input('get.');

		if(!empty($_input_data['table_id'])){
			$_table_info = $this->wy_content_api->get_table_info($_input_data['table_id']);
		}else{
			$this->error('系统错误，请刷新后再试!');exit;
		}
		if(!$_table_info){
			$this->error('系统错误，请刷新后再试!!');exit;
		}
		$_get_key = array('cid','table_id','status','push');
		if($_table_info['search']){
			foreach($_table_info['search'] as $k=>$v){
				$_get_key[] = $v['field_name'];
			}
		}
		$_get_str = "";
		if($_input_data){
			foreach($_input_data as $k=>$v){
				if(strpos($k,'-') !== false){
					$n_data = explode('-',$k);
					unset($_input_data[$k]);
					$_input_data[$n_data[0]][] = $v;
				}
			}
			foreach($_input_data as $k=>$v){
				if(is_array($v)){
					$_input_data[$k] = Array2String($v);
				}
			}
			foreach($_input_data as $k=>$v){
				$_v = trim($v);
				if(in_array($k,$_get_key)){
					if($_v != ''){
						$_get_str .= $k.":'".strval($_v)."',";
					}else{
						$_input_data[$k] = '';
					}
				}else{
					unset($_input_data[$k]);
				}
			}
		}
		if($_get_key){
			foreach($_get_key as $a){
				if(!isset($_input_data[$a])){
					$_input_data[$a] = '';
				}
			}
		}
		$this->assign('get_data',$_input_data);
		$this->assign('get_str',$_get_str);
		$this->assign('table_info',$_table_info);
		$cat_list = $this->wy_content_api->getSonLists();
		$this->assign('cat_lists',$cat_list);
		return $this->fetch();
	}
	/*
	 * 添加文章
	 *  需要对应 模型id table_id
	 */
	public function lists_add(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			$_ret = $this->add_article(0,$_post_data['table_id'],$_post_data);
			if($_ret['code']){
				$this->add_operation_log($_ret['id'],$_post_data);
				$this->success("添加成功");
			}else{
				$this->error($_ret['msg']);
			}
			exit;
		}
		$_input_data = input('get.');
		if(!empty($_input_data['table_id'])){
			$_table_info = $this->wy_content_api->get_table_info($_input_data['table_id']);
		}else{
			$this->error('系统错误，请刷新后再试!');exit;
		}
		if(!$_table_info){
			$this->error('系统错误，请刷新后再试!!');exit;
		}
		$cat_id = isset($_input_data['cat_id'])?$_input_data['cat_id']:'0';
		$_cat_info = array();
		if($cat_id > 0){
			$_cat_info = Db::name('content_cat')->where('id','=',$cat_id)->where('table_id','=',$_input_data['table_id'])->where('is_del','=',0)->find();
		}
		if(!$_cat_info){
			$cat_id = 0;
		}
		$_info = array(
			'table_id'	=> (int)$_input_data['table_id'],
			'id'		=> '0',
			'cat_id'	=> (int)$cat_id,
			'status'	=> '1',
			'onclick'	=> '0',
			'sort'		=> '0',
			'push_date'	=> date('Y-m-d H:i:s'),
		);
		if($_table_info['lists']){
			foreach($_table_info['lists'] as $k=>$v){
				$_info[$v['field_name']] = '';
			}
		}
		// print_r($_info);
		// print_r($_table_info['lists_group']);
		$this->assign('info',$_info);
		$this->assign('field_lists',$_table_info['lists_group']);

		// print_r($_table_info['type_group']);
		$this->assign('type_group',$_table_info['type_group']);

		if($_cat_info){
			$this->assign('page_name','添加文章 - '.$_cat_info['cat_name']);
		}else{
			$this->assign('page_name','添加文章');
		}
		$cat_list = $this->wy_content_api->getSonLists();
		$this->assign('loading_num',0);
		$this->assign('cat_lists',$cat_list);
		$this->assign('wy_layui_id',$_info['cat_id']);
		return $this->fetch('lists_info');
	}
	/*
	 * 编辑文章
	 *  需要对应 模型id table_id
	 */
	public function lists_edit(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');
			$_ret = $this->add_article($_post_data['id'],$_post_data['table_id'],$_post_data);
			if($_ret['code']){
				$this->add_operation_log($_ret['id'],$_post_data);
				$this->success("编辑成功");
			}else{
				$this->error($_ret['msg']);
			}
			exit;
		}
		$_input_data = input('get.');
		if(!empty($_input_data['table_id'])){
			$_table_info = $this->wy_content_api->get_table_info($_input_data['table_id']);
		}else{
			$this->error('系统错误，请刷新后再试!');exit;
		}
		if(!$_table_info){
			$this->error('系统错误，请刷新后再试!!');exit;
		}
		$cat_id = isset($_input_data['cat_id'])?$_input_data['cat_id']:'0';
		if(!empty($_input_data['id'])){
			$_info = Db::name('content_lists_'.$_input_data['table_id'])->find($_input_data['id']);
		}else{
			$this->error('系统错误，请刷新后再试!!');exit;
		}
		$_info['table_id']	= $_input_data['table_id'];
		// print_r($_info);exit;
		$this->assign('info',$_info);

		$_cat_info = Db::name('content_cat')->where('id','=',$_info['cat_id'])->where('table_id','=',$_info['table_id'])->find();


		$this->assign('field_lists',$_table_info['lists_group']);

		// print_r($_table_info['type_group']);
		$this->assign('type_group',$_table_info['type_group']);

		if($_cat_info){
			$this->assign('page_name','编辑文章 - '.$_cat_info['cat_name']);
		}else{
			$this->assign('page_name','编辑文章');
		}
		$cat_list = $this->wy_content_api->getSonLists();
		$this->assign('cat_lists',$cat_list);
		$this->assign('loading_num',0);
		$this->assign('wy_layui_id',$cat_id?$cat_id:$_info['cat_id']);
		return $this->fetch('lists_info');
	}
	/*
	 * 删除文章
	 *  需要对应 模型id table_id
	 *  post 是批量删除
	 *  get 是单独删除
	 */
	public function lists_del(){
		$_input_data = input('get.');
		if(!empty($_input_data['table_id'])){
			$_table_info = $this->wy_content_api->get_table_info($_input_data['table_id']);
		}else{
			$this->error('系统错误，请刷新后再试!');exit;
		}
		$_input_post = input('post.');
		if(!empty($_input_post['id'])){
			if(!(count($_input_post['id']) > 0)){
				$this->error('请选择需要删除的信息!');exit;
			}
			$_lists = Db::name('content_lists_'.$_input_data['table_id'])->where('id','in',$_input_post['id'])->select();
			if($_lists){
				$_ret = Db::name('content_lists_'.$_input_data['table_id'])->where('id','in',$_input_post['id'])->delete();
				if($_ret){
					$_all_id = [];
					foreach ($_lists as $key => $value) {
						$_all_id[] = $value['id'];
					}
					$this->add_operation_log($_all_id,$_lists);
					$this->success('删除成功');
				}
			}else{
				$this->error('系统错误，请刷新后再试!');exit;
			}
			$this->add_operation_log($_ret['id'],$_lists);
		}else{
			if(!empty($_input_data['id'])){
				$_data = Db::name('content_lists_'.$_input_data['table_id'])->where('id','=',$_input_data['id'])->select();
				if(!$_data){
					$this->error('系统错误，请刷新后再试!');exit;
				}
				$_ret = Db::name('content_lists_'.$_input_data['table_id'])->where('id','=',$_input_data['id'])->delete();
				if($_ret){
					$this->add_operation_log($_data['id'],$_data);
					$this->success('删除成功');
				}else{
					$this->error('系统错误，请刷新后再试!');exit;
				}
			}else{
				$this->error('系统错误，请刷新后再试!');exit;
			}
		}
	}
	/*
	 * 单页管理
	 *  attr_type 自定义属性
	 *
	 *  attr_type 自定义属性格式

			[{
				"field": "company_num", 		字段
				"field_type": "text", 			类型  text|textarea|images
				"field_name": "参股企业", 		名称
				"field_tips": "",				描述
				"field_group": ""				分组可空
			}, {
				"field": "info",				字段
				"field_type": "textarea",		类型  text|textarea|images
				"field_name": "中文描述", 		名称
				"field_tips": "",				描述
				"field_group": ""				分组可空
			}, {
				"field": "thumb",				字段
				"field_type": "images",			类型  text|textarea|images
				"field_name": "缩略图",        	名称
				"field_tips": "推荐宽590px*高689px",	描述
				"field_group": ""				分组可空
			}]
	 */
	public function pages(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_id =	$_post_data['id'];
			unset($_post_data['id']);

			$_post_data['attr_content'] = json_encode($_post_data['attr_content']);

			$_post_data['update_time'] = time();
			$_ret_id = Db::name('content_page')->where('id','=',$_id)->update($_post_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_post_data);
				$this->success("编辑成功");
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_input_data = input('get.');
		$_cat_info = Db::name('content_cat')->where('id','=',$_input_data['cid'])->where('is_del','=',0)->where('type','=',2)->find();
		if($_cat_info){
			$this->assign('page_name','编辑单页 - '.$_cat_info['cat_name']);
		}else{
			$this->error("系统错误，数据不存在！");
		}
		$_info = Db::name('content_page')->where('id','=',$_input_data['cid'])->find();
		$this->assign('info',$_info);

		// print_r($_cat_info);
		$_field_lists = array();
		if($_cat_info['attr_type']){
			$attr_type = json_decode($_cat_info['attr_type'],true);
			$attr_value = array();
			if($_info['attr_content']){
				$attr_value = json_decode($_info['attr_content'],true);
			}
			if($attr_type){
				foreach($attr_type as $k=>$v){
					$field_group = isset($v['field_group']) ? $v['field_group'] : '基础属性';
					$field_group = $field_group == '' ? '基础属性' : $field_group;
					$_field_lists[$field_group]['name'] = $field_group;

					$v['value'] = isset($attr_value[$v['field']]) ? $attr_value[$v['field']] : '';

					$_field_lists[$field_group]['lists'][] = $v;
				}
			}
		}
		$this->assign('field_lists',$_field_lists);
		return $this->fetch('pages');
	}

	public function links(){
		if($this->request->isPost() == true){
			$_post_data = input('post.');

			$_id =	$_post_data['id'];
			$_data = array();
			$_data['url'] = $_post_data['url'];

			$_data['update_time'] = time();
			$_ret_id = Db::name('content_cat')->where('id','=',$_id)->update($_post_data);
			if($_ret_id){
				$this->add_operation_log($_id,$_data);
				$this->success("编辑成功");
			}else{
				$this->error("编辑失败");
			}
			exit;
		}
		$_input_data = input('get.');
		$_cat_info = Db::name('content_cat')->where('id','=',$_input_data['cid'])->where('type','=',0)->where('is_del','=',0)->find();
		if($_cat_info){
			$this->assign('page_name','编辑友链 - '.$_cat_info['cat_name']);
		}else{
			$this->error("系统错误，数据不存在！");
		}
		$this->assign('info',$_cat_info);
		return $this->fetch('links');
	}

	/*
	 * 异步获取文章文章
	 *  需要对应 模型id table_id
	 */
	public function ajax_lists(){
		if($this->request->isAjax() != true){
			$this->error('404');
		}
		$_input_data = input();
		$page = $_input_data['page'] >= 1 ? (int)$_input_data['page'] : '0';
		$limit = $_input_data['limit'] >= 1 ? (int)$_input_data['limit'] : '15';
		unset($_input_data['page']);
		unset($_input_data['limit']);

		if(empty($_input_data['table_id'])){
			$this->error('404');
		}
		$_table_info = $this->wy_content_api->get_table_info($_input_data['table_id']);
		if(!$_table_info){
			$this->error('404');
		}
		$_field = 'id,cat_id,status,onclick,sort,push_date,create_time';
		$_function_field = array();
		$_function_field_lists = array();
		if($_table_info['nav']){
			foreach($_table_info['nav'] as $k=>$v){
				$_field .= ','.$v['field_name'];
				if(in_array($v['type'],array('radio','select','checkbox'))){
					$_function_field[$v['field_name']] = $v['attr_type'];
					$_function_field_lists[] = $v['field_name'];
				}
			}
		}
		$_where = array();
		$count = Db::name($_table_info['table_name'])->where($_where)->count();
		$lists = Db::name($_table_info['table_name'])->where($_where)
				->page($page.','.$limit)->field($_field)
				->order('sort desc,push_date desc,id desc')->select();
		if($lists){
			$_attr = new Attr();
			foreach($lists as $k=>$v){
				foreach($v as $aa=>$bb){
					if(in_array($aa,$_function_field_lists)){
						$lists[$k][$aa] = $_attr->get_field_value($_function_field[$aa],$bb);
					}
				}
				$lists[$k]['cat_name'] = $this->wy_content_api->get_cat_name($v['cat_id']);
				$lists[$k]['create_date'] = date('Y-m-d H:i:s',$v['create_time']);

				unset($lists[$k]['cat_id']);
				unset($lists[$k]['create_time']);
			}
		}
		$_json = array();
		$_json['code'] = 0;
		$_json['msg'] = 'success';
		$_json['count'] = $count;
		$_json['data'] = $lists;
		echo json_encode($_json);
		exit;
	}

	/*
	 * 私有方法，文章内容的添加修改用该方法
	 *  数据的校验，无效信息的剔除
	 */
	private function add_article($id=0,$table_id=0,$_post_data=array()){
		$_table_info = Db::name('content_model_table')->where('id','=',$table_id)->find();
		$_ret_data = array();
		if(!$_table_info){
			$_ret_data['code'] = 0;
			$_ret_data['info'] = '系统错误，数据库不存在';
			return $_ret_data;
		}
		$_table_name = 'content_lists_'.$_table_info['id'];
		unset($_post_data['id']);
		unset($_post_data['table_id']);

		foreach($_post_data as $k=>$v){
			if(strpos($k,'-') !== false){
				$n_data = explode('-',$k);
				unset($_post_data[$k]);
				$_post_data[$n_data[0]][$n_data[1]] = $v;
			}
		}

		$_field_lists = array(
			'title','cat_id','status','onclick','sort',
			'seo_title','seo_keywords','seo_description','push_date'
		);
		$_table_info = $this->wy_content_api->get_table_info($table_id);
		if($_table_info['lists']){
			foreach($_table_info['lists'] as $k=>$v){
				$_field_lists[] = $v['field_name'];
			}
		}

		// 过滤不存在的字段
		foreach($_post_data as $k=>$v){
			if(in_array($k,$_field_lists)){
				if(is_array($v)){
					$_post_data[$k] = Array2String($v);
				}
			}else{
				unset($_post_data[$k]);
			}
		}
		if($id == 0){
			$_post_data['create_time'] = time();
			$_post_data['update_time'] = time();
			$_ret_id = Db::name($_table_name)->insertGetId($_post_data);
			if($_ret_id){
				$_ret_data['code'] = 1;
				$_ret_data['id'] = $_ret_id;
				$_ret_data['msg'] = '添加成功';
				return $_ret_data;
			}else{
				$_ret_data['code'] = 0;
				$_ret_data['msg'] = '添加失败';
				return $_ret_data;
			}
		}else{
			$_post_data['update_time'] = time();
			$_ret_id = Db::name($_table_name)->where('id','=',$id)->update($_post_data);
			if($_ret_id){
				$_ret_data['code'] = 1;
				$_ret_data['id'] = $id;
				$_ret_data['msg'] = '编辑成功';
				return $_ret_data;
			}else{
				$_ret_data['code'] = 0;
				$_ret_data['msg'] = '编辑失败';
				return $_ret_data;
			}
		}
	}
}
?>